package org.example.subject2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author brant
 */
public class Solution {

    public static void main(String[] args) {
        ListNode l111111111 = new ListNode(9, null);
        ListNode l11111111 = new ListNode(9, l111111111);
        ListNode l1111111 = new ListNode(9, l11111111);
        ListNode l111111 = new ListNode(9, l1111111);
        ListNode l11111 = new ListNode(9, l111111);
        ListNode l1111 = new ListNode(9, l11111);
        ListNode l111 = new ListNode(9, l1111);
        ListNode l11 = new ListNode(9, l111);
        ListNode l1 = new ListNode(9, l11);

        ListNode l2222 = new ListNode(9, null);
        ListNode l222 = new ListNode(9, l2222);
        ListNode l22 = new ListNode(9, l222);
        ListNode l2 = new ListNode(9, l22);

        System.out.println(new Solution().addTwoNumbers(l1, l2));
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode pre = new ListNode(0);
        ListNode cur = pre;
        int remain = 0;
        while(l1 != null || l2 != null) {
            int x = l1 == null ? 0 : l1.val;
            int y = l2 == null ? 0 : l2.val;
            int sum = x + y + remain;

            remain = sum / 10;
            sum = sum % 10;
            cur.next = new ListNode(sum);

            cur = cur.next;
            if(l1 != null) {
                l1 = l1.next;
            }
            if(l2 != null) {
                l2 = l2.next;
            }
        }
        if(remain == 1) {
            cur.next = new ListNode(remain);
        }
        return pre.next;
    }

}
